package com.macro.mall.tiny.modules.bms.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.bms.model.Config;
import com.macro.mall.tiny.modules.bms.service.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "ConfigController", description = "公共配置管理")
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @Operation(summary = "根据ID获取配置详情")
    @Parameters({
            @Parameter(name = "id", description = "配置ID")
    })
    @GetMapping("/{id}")
    public CommonResult<Config> getById(@PathVariable Long id) {
        Config config = configService.getById(id);
        if (config == null || config.getDelFlag() == 1) {
            return CommonResult.failed("配置不存在");
        }
        return CommonResult.success(config);
    }

    @Operation(summary = "根据key获取配置")
    @Parameters({
            @Parameter(name = "key", description = "配置key")
    })
    @GetMapping("/getByKey/{key}")
    public CommonResult<Config> getByKey(@PathVariable String key) {
        Config config = configService.getByKey(key);
        if (config == null || config.getDelFlag() == 1) {
            return CommonResult.failed("配置不存在");
        }
        return CommonResult.success(config);
    }

    @Operation(summary = "分页获取配置列表")
    @Parameters({
            @Parameter(name = "key", description = "配置key，用于筛选", example = "site.name"),
            @Parameter(name = "pageNum", description = "页码", example = "1"),
            @Parameter(name = "pageSize", description = "每页数量", example = "10")
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<Config>> getList(
            @RequestParam(required = false) String key,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        CommonPage<Config> pageList = configService.getPageList(key, pageNum, pageSize);
        return CommonResult.success(pageList);
    }

    @Operation(summary = "新增配置")
    @PostMapping("/create")
    public CommonResult<Object> create(@RequestBody Config config) {
        boolean result = configService.create(config);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "更新配置")
    @PutMapping("/update/{id}")
    public CommonResult<Object> update(@PathVariable Long id, @RequestBody Config config) {
        boolean result = configService.update(id, config);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @Operation(summary = "删除配置")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        boolean result = configService.delete(id);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}